package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/ChecklistItem", "/checklistItem", "/ChecklistItemServlet", "/checklistItemServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class ChecklistItemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public ChecklistItemServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<ChecklistItemModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/checklistItemNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			ChecklistItemModel query = new ChecklistItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			ChecklistItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("checklistItem", obj);
				getServletContext().getRequestDispatcher("/checklistItemEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			ChecklistItemModel query = new ChecklistItemModel();
			String strId = request.getParameter("id");
			int idChecklist = 0;
			idChecklist = Integer.parseInt(request.getParameter("idChecklist"));
			String checklistUrl = "checklist.jsp?id=" + idChecklist;

			int id = Integer.parseInt(strId);
			query.setId(id);

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect(checklistUrl);
			} else {
				// TODO fail
				response.sendRedirect(checklistUrl);
			}
		} else if (mode.equalsIgnoreCase("view")) {

			ChecklistItemModel query = new ChecklistItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			ChecklistItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("checklistItem", obj);
				getServletContext().getRequestDispatcher("/checklistItemView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			ChecklistItemModel obj = new ChecklistItemModel();

			String id_checklist = request.getParameter("id_checklist");
			if (id_checklist != null && !id_checklist.isEmpty()) {
				obj.setIdChecklist(Integer.parseInt(id_checklist));
			}

			String checklistUrl = "checklist.jsp?id=" + id_checklist;

			String id_phase = request.getParameter("id_phase");
			if (id_phase != null && !id_phase.isEmpty()) {
				obj.setIdPhase(Integer.parseInt(id_phase));
			}
			
			String id_parent = request.getParameter("id_parent");
			if (id_parent != null && !id_parent.isEmpty()) {
				obj.setIdParent(Integer.parseInt(id_parent));
			}

			String display_index = request.getParameter("display_index");
			obj.setDisplayIndex(display_index);

			String name = request.getParameter("name");
			obj.setName(name);

			String remark = request.getParameter("remark");
			obj.setRemark(remark);

			String is_group = request.getParameter("is_group");
			obj.setIsGroup(is_group != null ? true : false);

			if (obj.getIsGroup() == true) {
				obj.setIdEmployee(Integer.MIN_VALUE);
				obj.setIdCompany(Integer.MIN_VALUE);
				obj.setStartTime(null);
				obj.setEndTime(null);
				obj.setStatus(Integer.MIN_VALUE);
			} else {
				String id_employee = request.getParameter("id_employee");
				if (id_employee != null && !id_employee.isEmpty()) {
					obj.setIdEmployee(Integer.parseInt(id_employee));
				}

				String id_company = request.getParameter("id_company");
				if (id_company != null && !id_company.isEmpty()) {
					obj.setIdCompany(Integer.parseInt(id_company));
				}

				String start_time_date = request.getParameter("start_time_date");
				// String start_time_time =
				// request.getParameter("start_time_time");
				if (start_time_date != null && !start_time_date.isEmpty()) {
					obj.setStartTime(DateTimeUtil.parseDateTime(start_time_date));
				}

				String end_time_date = request.getParameter("end_time_date");
				// String end_time_time = request.getParameter("end_time_time");
				if (end_time_date != null && !end_time_date.isEmpty()) {
					obj.setEndTime(DateTimeUtil.parseDateTime(end_time_date));
				}

				String status = request.getParameter("status");
				if (status != null && !status.isEmpty()) {
					obj.setStatus(Integer.parseInt(status));
				}

				// String index = request.getParameter("index");
				// if (index != null && !index.isEmpty()) {
				// obj.setIndex(Integer.parseInt(index));
				// }
				//
				// String type = request.getParameter("type");
				// if (type != null && !type.isEmpty()) {
				// obj.setType(Integer.parseInt(type));
				// }

				// String is_draft = request.getParameter("is_draft");
				// obj.setIsDraft(is_draft.equalsIgnoreCase("true")
				// || is_draft.equalsIgnoreCase("1"));

				// String notification = request.getParameter("notification");
				// if (notification != null && !notification.isEmpty()) {
				// obj.setNotification(Integer.parseInt(notification));
				// }
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					response.sendRedirect(checklistUrl);
				} else {
					// TODO : processing error here
					response.sendRedirect(checklistUrl);
				}
			} else {

				// invalid input

				request.setAttribute("checklistItem", obj);
				getServletContext().getRequestDispatcher("/checklistItemInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			ChecklistItemModel query = new ChecklistItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			ChecklistItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// String id_checklist = request.getParameter("id_checklist");
				// obj.setIdChecklist(Integer.parseInt(id_checklist));
				
				String id_phase = request.getParameter("id_phase");
				if (id_phase != null && !id_phase.isEmpty()) {
					obj.setIdPhase(Integer.parseInt(id_phase));
				}

				String id_parent = request.getParameter("id_parent");
				if (id_parent != null && !id_parent.isEmpty()) {
					obj.setIdParent(Integer.parseInt(id_parent));
				}

				String display_index = request.getParameter("display_index");
				obj.setDisplayIndex(display_index);

				String name = request.getParameter("name");
				obj.setName(name);

				String remark = request.getParameter("remark");
				obj.setRemark(remark);

				String is_group = request.getParameter("is_group");
				obj.setIsGroup(is_group != null ? true : false);

				if (obj.getIsGroup() == true) {
					obj.setIdEmployee(Integer.MIN_VALUE);
					obj.setIdCompany(Integer.MIN_VALUE);
					obj.setStartTime(null);
					obj.setEndTime(null);
					obj.setStatus(Integer.MIN_VALUE);
				} else {
					String id_employee = request.getParameter("id_employee");
					obj.setIdEmployee(Integer.parseInt(id_employee));

					String id_company = request.getParameter("id_company");
					obj.setIdCompany(Integer.parseInt(id_company));

					String start_time_date = request.getParameter("start_time_date");
					// String start_time_time =
					// request.getParameter("start_time_time");
					if (start_time_date != null && !start_time_date.isEmpty()) {
						obj.setStartTime(DateTimeUtil.parseDateTime(start_time_date));
					}

					String end_time_date = request.getParameter("end_time_date");
					// String end_time_time =
					// request.getParameter("end_time_time");
					if (end_time_date != null && !end_time_date.isEmpty()) {
						obj.setEndTime(DateTimeUtil.parseDateTime(end_time_date));
					}

					String status = request.getParameter("status");
					obj.setStatus(Integer.parseInt(status));

					// String index = request.getParameter("index");
					// obj.setIndex(Integer.parseInt(index));
					//
					// String type = request.getParameter("type");
					// obj.setType(Integer.parseInt(type));
					//
					// String is_draft = request.getParameter("is_draft");
					// obj.setIsDraft(is_draft.equalsIgnoreCase("true")
					// || is_draft.equalsIgnoreCase("1"));

					// String notification =
					// request.getParameter("notification");
					// obj.setNotification(Integer.parseInt(notification));
				}

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						String checklistUrl = "checklist.jsp?id=" + obj.getIdChecklistToString();
						response.sendRedirect(checklistUrl);
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("checklistItem", obj);
					getServletContext().getRequestDispatcher("/checklist.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			ChecklistItemModel query = new ChecklistItemModel();
			String id = request.getParameter("id");
			int idChecklist = 0;
			idChecklist = Integer.parseInt(request.getParameter("idChecklist"));
			String checklistUrl = "checklist.jsp?id=" + idChecklist;

			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect(checklistUrl);
			} else {
				// TODO fail
				response.sendRedirect(checklistUrl);
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			ChecklistItemModel obj = new ChecklistItemModel();

			// TODO set name here

			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}